package cn.itcast.bos.web.action.system;

import cn.itcast.bos.domain.system.Menu;
import cn.itcast.bos.domain.system.User;
import cn.itcast.bos.service.system.MenuService;
import cn.itcast.bos.web.action.common.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;

import java.util.List;

@Namespace("/")
@ParentPackage("json-default")
@Controller
@Scope("prototype")
public class MenuAction extends BaseAction<Menu> {

    @Autowired
    private MenuService menuService;

    //menu列表查询menu_list.action
    @Action(value = "menu_list",results = {@Result(name = "success",type = "json")})
    public String findList(){
        List<Menu> list =  menuService.findAll();
        ActionContext.getContext().getValueStack().push(list);
        return SUCCESS;
    }

    //保存方法menu_save.action
    @Action(value = "menu_save",results =
            {@Result(name = "success",type = "redirect",location = "pages/system/menu.html")})
    public String save(){
        menuService.save(model);
        return SUCCESS;
    }

    //查询列表菜单显示menu_show.action
    @Action(value = "menu_show",results = {@Result(name = "success",type = "json")})
    public String findMenu(){
        //获取当前登录用户
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        List<Menu> list = menuService.findMenuByUser(user);
        ActionContext.getContext().getValueStack().push(list);
        return SUCCESS;
    }

}
